#pragma once

#include "noncopyable.h"
#include "Timestamp.h"

#include <functional>
#include <memory>
#include "EventLoop.h"
#include <mutex>
#include "Logger.h"
#include "Common.h"
#include "Buffer.h"
#include <atomic>
class EventLoop;
struct Message
{
    uint8_t buffer[1024];
    int len;
};

class Channel : noncopyable
{
public:
    typedef std::shared_ptr<Channel> ptr;
    Channel(int& fd);
    ~Channel();
    // 设置回调函数对象
    void setHandleCallback(callback cb) { m_handle_call_back = cb; }
    // 防止当channel被手动remove掉，channel还在执行回调操作
    int getfd() const { return m_fd; }
    void setfd(const int& fd){m_fd=fd;}
    int events() const { return events_; }
      // 返回fd当前的事件状态
    bool isNoneEvent() const { return events_ == kNoneEvent; }
    bool isWriting() const { return events_ & kWriteEvent; }
    bool isReading() const { return events_ & kReadEvent; }
    // 设置fd相应的事件状态
    void enableReading(){events_ |= kReadEvent;}
    void disableReading(){events_ &= ~kReadEvent;}
    void enableWriting(){events_ |= kWriteEvent;}
    void disableWriting(){events_ &= ~kWriteEvent;;}
    void disableAll(){events_ = kNoneEvent;}
public:
    static const int kNoneEvent;
    static const int kReadEvent;
    static const int kWriteEvent;

    int& m_fd;                          // fd, Poller监听的对象
    int events_;                      // 注册fd感兴趣的事件
    callback m_handle_call_back;
};
